using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sustainalytics.Synchronization.Validation.Validators;

namespace Sustainalytics.Synchronization.Validation.Log
{
    public interface IMessageFormatter
    {
        string Format(ValidationFailure failure);

        CorporateDataInfo GetCorporateDataInfo(ValidationFailure failure);

        Task LoadAdditionalDataAsync(IEnumerable<Guid> companyIds);
    }

    public class MessageFormatter : IMessageFormatter
    {
        private readonly CorporateDataLoader loader;

        public MessageFormatter(CorporateDataLoader loader)
        {
            this.loader = loader;
        }

        public Task LoadAdditionalDataAsync(IEnumerable<Guid> companyIds)
        {
            return loader.LoadDataAsync(companyIds);
        }

        public string Format(ValidationFailure failure)
        {
            var companyInfo = loader.GetCorporateDataInfo(failure.Key);
            return string.Format(failure.ValidationError, companyInfo.CompanyName, companyInfo.CapitalIqId);
        }

        public CorporateDataInfo GetCorporateDataInfo(ValidationFailure failure)
        {
            return loader.GetCorporateDataInfo(failure.Key);
        }
    }
}
